/**
 * Created by Michael on 2017/2/13.
 */
//创建控制器
(function (angular) {
    var app=angular.module('mainApp.in_theaters',[])
    app.controller('in_theatersCtrl', ['$scope','jsonService','$routeParams','$route',
        function ($scope,jsonService,$routeParams,$route) {
            console.log(1);
            //标题
            $scope.title='正在加载中...';
            //电影列表数据
            $scope.subjects=[];
            //电影数据总条数
            $scope.total=0;
            //定义每一页面的容量
            $scope.pageCount=5;
            //总页数
            $scope.pageTotal=0;
            //当前页数，$routeParams.page获取路由when中可变的参数
            $scope.pageCurrent=parseInt($routeParams.page || '1');
            //上一页
            $scope.upPage= function () {
                if($scope.pageCurrent>1){
                    $scope.pageCurrent-=1
                    //$route.updateParams更新url地址中的page值，有就修改没有就添加
                    $route.updateParams({"page":$scope.pageCurrent})
                }

            }
            //下一页
            $scope.downPage= function () {
                if($scope.pageCurrent<$scope.pageTotal){
                    $scope.pageCurrent+=1;
                    $route.updateParams({"page":$scope.pageCurrent})
                }

            }

            var url='https://api.douban.com/v2/movie/'+$routeParams.type
            var obj={
                'apikey':'00fa6c0654689a0202ef4412fd39ce06',
                'start':($scope.pageCurrent-1)* $scope.pageCount,
                'count': $scope.pageCount,
                'q':$routeParams.q
            }

            jsonService.getJson(url,obj, function (data) {
                $scope.title=data.title;
                $scope.subjects=data.subjects;

                //电影数据总条数
                $scope.total=data.total;

                console.log($scope.subjects);
                console.log(data);
                //获取总页数
                $scope.pageTotal=Math.ceil($scope.total/$scope.pageCount)
                //异步加载需要$apply()通知angular数据更新
                $scope.$apply();


            });
        }]);

})(angular)